package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.PeachblossomApi;
import com.tanhua.dubbo.server.pojo.Peachblossom;

import com.tanhua.server.vo.PeachblossomVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class PeachblossomService {
    @Value("${fdfs.web-server-url}")
    private String weburl;
    @Autowired
    protected FastFileStorageClient storageClient;
    @Reference(version = "1.0.0")
    private PeachblossomApi peachblossomApi;
    @Autowired
    private UserInfoService userInfoService;
    //保存桃花传音
    public String savePeachblossom(MultipartFile soundFile) {
        User user = UserThreadLocal.get();
        Peachblossom peachblossom = new Peachblossom();
        //保存id
        peachblossom.setId(Math.toIntExact(user.getId()));
        try {
            //上传时间
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(currentTime);
            peachblossom.setStarttime(dateString);
            //上传桃花传音
            StorePath storePath = storageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(),
                    StringUtils.substringAfter(soundFile.getOriginalFilename(), "."), null);
            peachblossom.setSoundUrl(weburl+ "/" +storePath.getFullPath());
           // peachblossom.setPeachblossomID(ObjectId.get());
            Integer integer = peachblossomApi.savePeachblossom(peachblossom);
            return integer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }
    //获取桃花传音
    public PeachblossomVo getPeachblossom() {
        //获取不是自己发的桃花传音
        User user = UserThreadLocal.get();
        try {
            Peachblossom peachblossom = peachblossomApi.getPeachblossom(Math.toIntExact(user.getId()));
            //获取时间
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(currentTime);
            //对PeachblossomVo封装
            PeachblossomVo peachblossomVo = new PeachblossomVo();
            UserInfo userInfo = userInfoService.queryUserInfoByUserId(Long.valueOf(peachblossom.getId()));
            //id,接受的桃花传音作者的id
            peachblossomVo.setId(peachblossom.getId());
            //头像
            peachblossomVo.setAvatar(userInfo.getLogo());
            //昵称
            peachblossomVo.setNickname(userInfo.getNickName());
            //性别
            peachblossomVo.setGender(userInfo.getSex().name().toLowerCase());
            //年龄
            peachblossomVo.setAge(userInfo.getAge());
            //语音地址
            peachblossomVo.setSoundUrl(peachblossom.getSoundUrl());
            //剩余次数
            peachblossomVo.setRemainingTimes(peachblossom.getGETremainingTimes());
            return peachblossomVo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
}
